#!/bin/bash
# 交互式-安装docker仓库(docker-distribution) 安装docker-ce 设置docker相关配置 显示docker相关命令

readonly functions_name="public_functions" #文件名称
readonly functions_url="$(find / -path /proc -prune -o -name $functions_name -print)" #获取文件路径

if [[ -n $functions_url ]]; then #查找函数文件并加载

    for i in $functions_url; do . $i; done
else
    echo "Not fount $functions_name"
fi

##1 安装docker仓库
docker_Distribution_install() {

  rpm -q docker-distribution
  if [[ $? -eq 0 ]]; then
    logTips "docker-distribution already installed" "" #1-20#
    logY "rpm -qa | grep docker-distribution --color"
    rpm -qa | grep docker-distribution --color
    echo && exit
  fi

  logW "1. 安装docker仓库(docker-distribution)" #1-20#
  logY "yum install docker-distribution -y" #1-20#
    yum install docker-distribution -y
  checkTF #21#

  logW "2. 启动docker-distribution服务(设置成开机启动)" #1-20#
  logY "systemctl enable --now docker-distribution" #1-20#
    systemctl enable --now docker-distribution
  checkTF #21#

  logW "3. 检测防火墙，若是开启状态，放行docker-distribution" #1-20#
  logY "get_Firewalld_status #1-20#" #1-20#
  get_Firewalld_status #23#
    if [[ $? -eq 0 ]]; then
      firewall-cmd --add-service=docker-registry --permanent 2>/dev/null
      echo
    else
      logR "防火墙未开启 \n" #1-20#
    fi

  logW "4. 访问仓库..." #1-20#
  logY "curl 127.0.0.1:5000/v2/_catalog" #1-20#
    curl 127.0.0.1:5000/v2/_catalog
  checkTF #21#
}

##2 安装docker-ce
docker_Ce_install() {

  # 判断docker-ce是否已经安装
  if [[ $(rpm -qa | grep docker-ce-[0-9].*) ]]; then
    logTips "docker-ce already installed" "" #6#
    logY "rpm -qa | grep -E \"docker-ce|containerd.io\""
    rpm -qa | grep -E "docker-ce|containerd.io" --color
    echo && exit
  fi

  docker_repo_url="https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo"

  logW "1. 准备docker-ce yum源" #1-20#
  logY "curl -# --output /etc/yum.repos.d/docker-ce.repo ${docker_repo_url}" #1-20#
    if [[ ! -f /etc/yum.repos.d/docker-ce.repo ]]; then
      curl -# --output /etc/yum.repos.d/docker-ce.repo ${docker_repo_url}
    fi
    yum repolist | grep -E "repo id|docker-ce" --color
  checkTF #21#

  logW "2. 开始安装docker-ce" #1-20#
  logY "yum install docker-ce-cli-18.09.9 containerd.io docker-ce-18.09.9 -y" #1-20#
    yum clean all
    for i in docker-ce-cli-18.09.9 containerd.io docker-ce-18.09.9; do
      yum install $i -y
      checkTF #21#
    done

  logW "3. 启动docker服务" #1-20#
  logY "systemctl enable --now docker" #1-20#
    systemctl enable --now docker
  checkTF #21#

  logW "4. 显示docker版本信息" #1-20#
  logY "docker --version" #1-20#
    docker --version
  checkTF #21#
}

##3 设置daemon.json文件
set_Daemon_json() {

  logW "1. 创建daemon.json文件并写入内容" #1-20#
  logY "cat >/etc/docker/daemon.json <-EOF" #1-20#
cat >/etc/docker/daemon.json <<-EOF
  {
    "exec-opts": ["native.cgroupdriver=systemd"],
    "registry-mirrors": ["https://docker.1panel.live"],
    "insecure-registries": ["180.76.232.94:5000","registry:5000"]
  }
EOF
  checkTF #21#

  logW "2. 重启docker" #1-20#
  logY "systemctl restart docker" #1-20#
    systemctl restart docker
  checkTF #21#

  logW "3. 查看daemon.json内容" #1-20#
  logY "cat /etc/docker/daemon.json" #1-20#
    cat /etc/docker/daemon.json
  checkTF #21#
}

##4 开启地址转换
open_Ip_forward() {

  logG "1. 开启IP地址转换" #1-20#
  logY "net.ipv4.ip_forward = 1" #1-20#
    cat /etc/sysctl.conf  | grep -E '^net\.ipv4\.ip_forward = (0|1)$' 2>/dev/null
    if [[ $? -eq 0 ]]; then
      sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/' /etc/sysctl.conf
      sysctl -p
    else
      sed -i '$a net.ipv4.ip_forward = 1' /etc/sysctl.conf
      sysctl -p
    fi
}

##5 docker常用命令
docker_Command() {

  echo -e "\e[33m docker常用命令列表↓↓ \e[0m"
  echo
  echo "   docker info						//查看docker信息"  
  echo "   docker inspect ubuntu:18.04				//查看镜像信息"
  echo "   docker inspect -f {{".Architecture"}} ubuntu:18.04	//精确查看信息"
  echo
  echo "   docker pull ubuntu:18.04				//下载ubuntu"
  echo "   pull hub.c.163.com/public/ubuntu:18.04		//从163下载ubuntu"
  echo "   docker tag ubuntu:latest myubuntu:latest		//打标签"
  echo "   docker push NAME[:TAG] && docker push test:latest"	//上传
  echo "   docker save -o ubuntu_18.04.tar ubuntu:18.04		//导出镜像"
  echo "   docker load -i ubuntu_18.04.tar			//加载镜像"
  echo "   docker load < ubuntu_18.04.tar			//加载镜像"
  echo "   docker commit -m \"Addid a new file\" -a \"Docker Newbee\" c547e35fa1fd  test:0.1"
  echo "   docker build -t Dockerfil"
  echo "   cat ubuntu-18.04-x86_64-minimal.tar.gz | docker import - ubuntu:18.04"
  echo "   docker image help"
  echo "   docker run -itd ubuntu:18.04"
  echo "   docker run ubuntu:18.04 /bin/ls /"
  echo "   docker container wait containerID"
  echo "   docker logs containerID --details -ft"
  echo "   docker (pause unpause stop start restart prune) containerID"
  echo "   docker (attach exec -it rm) containerID"
  echo "   journalctl -u docker.service"  
  echo "   docker history / search / rm / rmi / prune"
  echo "   docker inspect /top /stats containerID"
  echo "   docker cp / diff / port / update containerID"
}

##6 查看仓库镜像详情
repoistry_Curl() {

  echo -n "请按 127.0.0.1:5000 的格式输入ip地址和端口号: "
  read repoip
  ## 对输入的参数进行检查
  echo $repoip | grep -E '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{1,6}$'
  if [[ $? -ne 0 ]]; then
    logR "输入不合法, 请重新输入!" && sleep 1 #1-20#
    repoistry_Curl ##6
  fi
  imageInfo=$(curl $repoip/v2/_catalog 2>/dev/null)
  imageName=$(echo $imageInfo | tr -d '{}[]""'| awk -F ":" '{print $2}' | tr "," ' ')
  imageCount=`echo $imageName | xargs -n1 | wc -l`
  
  ##统计镜像数量##
  if [[ "$imageName" != "" ]]; then
    echo -e "仓库共有$imageCount个镜像"
  else
    logR "仓库为空" && curl $repoip/v2/_catalog 2>/dev/null #1-20#
  fi

  ##显示所有镜像的版本号##
  for i in $imageName
  do
    echo -e "`curl $repoip/v2/$i/tags/list 2>/dev/null`"
  done
}

##7 菜单
print_Start_log() {

  systemVersionname=$(cat /etc/redhat-release)

  echo -e "
——————————————————————————————————————————————————————————————————————————————————————————————————————————
                  欢迎使用Linux工具箱 此系统版本：$systemVersionname
——————————————————————————————————————————————————————————————————————————————————————————————————————————
      Docker相关
—————————————————————
\e[1;33m
           1) 安装docker仓库      2) 安装docker     3) 设置daemon.json    4) open_ip_forward
           5) 查看docker仓库      6)                7) docker常用命令     8) 返回主菜单            \e[0m
——————————————————————————————————————————————————————————————————————————————————————————————————————————"
}

##8 脚本执行入口
shell_On_start() {

  print_Start_log ##7

  echo -n "请输入操作编号: "
  read menu_Num

  case $menu_Num in

    1)#安装docker仓库
      docker_Distribution_install ;; ##1

    2)#安装docker-ce
      docker_Ce_install ;; ##2

    3)#设置daemon.json文件
      set_Daemon_json ;; ##3

    4)#开启地址转换
      open_Ip_forward ;; ##4

    5)#查看docker仓库
      repoistry_Curl ;; ##6

    6)#
      sh docker.sh ;;

    7)#
      docker_Command ;; ##5

    8)#返回主菜单
      sh menu.sh ;;

    *)
      logR "编号输入错误!! 请重新输入!!" && sleep 1 && clear && shell_On_start ;;
  esac
}

shell_On_start ##8
